package com.service.impl;

import com.dao.MemberbuyrecordDao;
import com.dto.BaseDto;
import com.dto.dataenum.MessageEnum;
import com.entity.Condition;
import com.entity.Memberbuyrecord;
import com.page.PageInfo;
import com.service.MemberbuyrecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;

@Service
public class MemberbuyrecordServiceImp implements MemberbuyrecordService {
    @Resource
    MemberbuyrecordDao memberbuyrecordDao;
    public BaseDto getAll()throws Exception{
        //枚举的具体值由三元表达式决定
        List<Memberbuyrecord> memberbuyrecords = memberbuyrecordDao.getAll();
        MessageEnum messageEnum = memberbuyrecords.size()>0?MessageEnum.GETAllMEMBERSUCCESS:MessageEnum.ADDMEBERFAIl;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        dto.settList(memberbuyrecords);//将查询结果也放入dto
        return dto;
    }

    /**
     *
     * @param pageInfo
     * @return
     */
    public BaseDto getMemberbuyrecordsByPage(PageInfo pageInfo){
        //枚举的具体值由三元表达式决定
        List<Memberbuyrecord> memberbuyrecords = memberbuyrecordDao.getMemberbuyrecordsByPage(pageInfo);
        MessageEnum messageEnum = memberbuyrecords.size()>0?MessageEnum.GETMEMBERSBYPAGESUCCESS:MessageEnum.GETMEMBERSBYPAGEFAIl;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        dto.settList(memberbuyrecords);//将查询结果也放入dto
        return dto;
    }
    /**
     * 搜索分页查询
     * @param pageInfo
     * @return
     */
    public BaseDto  searchMemberbuyrecordsByPage(PageInfo pageInfo, List<Condition> conditions) throws ParseException {
//        //枚举的具体值由三元表达式决定
//        List<Memberbuyrecord> memberbuyrecords = memberbuyrecordDao.searchMemberbuyrecordsByPage(pageInfo,conditions);
//        MessageEnum messageEnum = memberbuyrecords.size()>0?MessageEnum.SEARCHMEMBERSBYPAGESUCCESS:MessageEnum.SEARCHMBERSBYPAGEFAIl;
//        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
//        dto.settList(memberbuyrecords);//将查询结果也放入dto
//        return dto;
        return null;
    }

    public BaseDto getMemberbuyrecordByMno(Integer mbno)throws Exception{

        Memberbuyrecord memberbuyrecord = memberbuyrecordDao.getMemberbuyrecordByMno(mbno);
        MessageEnum messageEnum = Optional.ofNullable(memberbuyrecord).isPresent()?MessageEnum.GETMEMBERBYMNOSUCCESS:MessageEnum.GETMEMBERBYMNOFAIL;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        dto.setT(memberbuyrecord);//单个对象放入dto
        return dto;
    }

    public BaseDto add(Memberbuyrecord memberbuyrecord) throws Exception{
        Integer result = memberbuyrecordDao.add(memberbuyrecord);
        MessageEnum messageEnum = result>0?MessageEnum.ADDMEMBERSUCCESS:MessageEnum.ADDMEBERFAIl;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        return dto;
    }

    public BaseDto update(Memberbuyrecord memberbuyrecord) throws Exception{
        Integer result = memberbuyrecordDao.update(memberbuyrecord);
        MessageEnum messageEnum = result>0?MessageEnum.UPDATEMEMBERSUCCESS:MessageEnum.UPDATEMEBERFAIl;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        return dto;
    }
}
